package com.sfh.base;

import android.os.Environment;


import com.sfh.base.http.server.RxHttpClient;

import java.io.File;

import rx.Subscription;
import rx.functions.Action1;

/**
 * 功能描述:功能描述:静态变量
 *
 * @date 2017/6/6
 */
public class ApplicationEntity {

    private AbstractBaseApplication application;

    /*** 缓存文件夹路径*/
    private File cache;

    //服务器时间
    private  long sysTime = -1l;

    //上次请求时间
    private  long lastTime = 0l;


    /*** 获取服务器时间*/
    public long querySystemNowTime() {

        if (this.sysTime != -1) {
            //服务器时间= 当前手机时间 - 手机请求时时间 + 服务器时间
            long nowTime = System.currentTimeMillis ();
            this. sysTime = this.sysTime + (nowTime - this.lastTime);
            this.lastTime = nowTime;
        }
        return this.lastTime;
    }

    /*** 设置服务器时间*/
    public void setSystemNowTime(long time) {

        this.sysTime = time;
        this.lastTime = System.currentTimeMillis ();
    }


    public File getCache() {

        return cache;
    }

    ApplicationEntity() {

    }

    private static class Hondler {

        private static final ApplicationEntity app = new ApplicationEntity ();
    }

    private ApplicationEntity inject(AbstractBaseApplication application) {

        this.application = application;
        return this;
    }

    public static AbstractBaseApplication getApplication() {

        return Hondler.app.application;
    }

    public static ApplicationEntity getInstace() {

        return Hondler.app;
    }


    /***
     * 发送一个新的事件
     */
    public static void rxBusPostUI(Object event) {

        getApplication ().postRxBus (event);
    }

    /***
     * 注册【根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 】
     * 对应的注销借口
     * @Link unRegisterRxBus()
     * @param eventClass
     * @param onNext
     * @param <T>
     * @return
     */
    public static <T> Subscription rxBusregisterUI(final Class<T> eventClass, Action1<T> onNext) {

        return getApplication ().registerRxBus (eventClass, onNext);
    }


    public static class Creator {


        AbstractBaseApplication application;

        boolean isDebug;

        String apiURL;

        File cache;

        public Creator setDebug(boolean debug) {

            isDebug = debug;
            return this;
        }

        public Creator setURL(String apiURL) {

            this.apiURL = apiURL;
            return this;
        }

        /***
         * 设置APP 缓存文件夹
         * @param name 文件夹名
         * @return
         */
        public Creator setCache(String name) {
            //目录
            if (Environment.MEDIA_MOUNTED.equals (Environment.getExternalStorageState ())) {
                cache = new File (Environment.getExternalStorageDirectory () + File.separator + name);
            } else {
                // 没有内存卡 /cache
                cache = Environment.getDownloadCacheDirectory ();
                if (!cache.exists ()) {
                    cache = Environment.getDataDirectory ();
                }
            }

            // 创建目录
            if (!cache.exists ()) {
                cache.mkdirs ();
            }
            return this;
        }


        public Creator(AbstractBaseApplication context) {

            this.application = context;
        }


        public ApplicationEntity build() {

            ApplicationEntity app = Hondler.app.inject (application);
            Hondler.app.cache = cache;
            LogUtil.setDEBUG (isDebug);
            RxHttpClient.build (apiURL, isDebug);//初始化网络
            return app;
        }


    }
}
